////////////////////////////////////////
//	File Header
//	Author: Stephen Ringer | stephen.ringer@gmail.com
//	File: SAGANetworkDevice.h
//
////////////////////////////////////////
#pragma once
////////////////////////////////////////
//	File Includes
#include <windows.h>
#include <stdio.h>
#include "../SAGAD3D/SAGA.h"
//
////////////////////////////////////////

////////////////////////////////////////
//	Macros
#define WM_SAGASERVER (WM_USER + 1)
#define WM_SAGACLIENT (WM_USER + 2)
//
////////////////////////////////////////

////////////////////////////////////////
//	Struct Name: SAGAPACKAGE
//	Info:
//		nType reserved:
//		0 - server message to new client giving its ID, pData = UINT nID
//		1 - new client connected, pData = UINT nID
//		2 - client disconnected,  pData = UINT nID
typedef struct SAGAPACKAGE_TYPE 
{
	UINT   nLength;    // length of pData field in bytes
	UCHAR  nType;      // use for type (0 is ID notification)
	UINT   nSender;    // put senders ID here (0 is server)
	void  *pData;      // actual data of nLength
} SAGAPACKAGE;
//
////////////////////////////////////////

////////////////////////////////////////
//	Class Name: SAGANetworkDevice
class SAGANetworkDevice 
{
protected:
	HWND       m_hWndMain;          // application main window
	HINSTANCE  m_hDLL;              // dll module handle
	bool       m_bRunning;          // after succesful initialization
	int        m_nPort;             // port for communication
	char       m_pIP[256];          // IP address (valid for clients)
	UINT       m_nMaxSize;          // max package size for send/receive
	FILE      *m_pLog;              // log file
      
      
public:
	SAGANetworkDevice() {};
	virtual ~SAGANetworkDevice(){};


	// INIT/RELEASE STUFF:
	// ===================

	// feed: main window, server or client, port, ip (for clients only)
	//       max size of packages send over network, flush log
	virtual HRESULT Init(HWND, SAGANETMODE, int nPort, char *pIP, 
					   UINT nMaxPkgSize, UINT NumMax, bool)=0;

	virtual void    Release()=0;
	virtual bool    IsRunning()=0;

	// proceed incoming notifications from windows
	virtual HRESULT MsgProc(WPARAM, LPARAM)=0;

	// send methods for clients and servers
	virtual HRESULT SendToServer(const SAGAPACKAGE*)=0;
	virtual HRESULT SendToClients(const SAGAPACKAGE*)=0;
	virtual HRESULT SendToClient(const SAGAPACKAGE*,UINT)=0;

	// get information about queued packages
	virtual bool    IsPkgWaiting()=0;
	virtual UINT    GetNextPkgSize()=0;
	virtual HRESULT GetNextPkg(SAGAPACKAGE*)=0;
}; 
typedef class SAGANetworkDevice *LPSAGANETWORKDEVICE;
//
////////////////////////////////////////

////////////////////////////////////////
//	External DLL Functions
extern "C" 
{
	HRESULT CreateNetworkDevice(HINSTANCE hDLL, SAGANetworkDevice **pInterface);
	typedef HRESULT (*CREATENETWORKDEVICE)(HINSTANCE hDLL, SAGANetworkDevice **pInterface);

	HRESULT ReleaseNetworkDevice(SAGANetworkDevice **pInterface);
	typedef HRESULT (*RELEASENETWORKDEVICE)(SAGANetworkDevice **pInterface);
}
//
////////////////////////////////////////